package com.core.quartz;

import java.io.IOException;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.core.util.DspUtil;
import com.dsp.bean.Calc;
import com.dsp.bean.Jms;
import com.dsp.bean.Jobs;
import com.dsp.bean.JobsMessage;
import com.dsp.services.CalcService;
import com.dsp.services.JmsService;
import com.dsp.services.JobsMessageService;
import com.dsp.services.JobsService;

public class CalcQuartz {

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	private static boolean isTrue = true;

	public void work() {
		logger.debug(new Timestamp(System.currentTimeMillis()));
		if (isTrue == false) {
			logger.info("后台正在运行........");
			return;
		}
		JobsService jobsService = (JobsService) DspUtil.getBean("jobsService");
		DetachedCriteria dc = DetachedCriteria.forClass(Jobs.class);
		dc.add(Restrictions.eq("jobsStatus", 0)); // 查找未运行的状态
		List<Jobs> taskList = jobsService.likeBy(dc);
		if (DspUtil.isEmptyList(taskList)){
			isTrue = true;
			return;
		} else {
			isTrue = false;
		}
		InputStream is;
		Object objData = null;
		JmsService jms = (JmsService)DspUtil.getBean("jmsService");
		for (Jobs job : taskList) {
			Integer rtnStatus = 1;
			job.setStartDate(new Timestamp(System.currentTimeMillis()));
			job.setJobsStatus(-1); //修改这正在运行状态
			try {
				jobsService.update(job);
				is = job.getParasOne().getBinaryStream();
				ObjectInputStream ois = new ObjectInputStream(is);
				objData = ois.readObject();
				if ("calc".equalsIgnoreCase(job.getJobsType())) { // 社保计算
					rtnStatus = this.calc(job.getId(), objData);
				}
				job.setJobsStatus(rtnStatus);
				job.setEndDate(new Timestamp(System.currentTimeMillis()));
				jobsService.update(job);
				insertJMS(jms, "jobs_message?id="+job.getId() , "社保计算作业" , job.getUserId() , job.getCreateDate());
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		isTrue = true;
	}

	/**
	 * 社保计算
	 * 
	 * @param job
	 */
	private Integer calc(Long jobId, Object objData) {
		logger.debug("..............正在运行社保计算");
		Integer rtnStatus = 0;
		List<JobsMessage> error = new ArrayList<JobsMessage>();
		CalcService cs = (CalcService) DspUtil.getBean("calcService");
		try {
			Calc calc = (Calc) objData;
			logger.debug(calc);
			rtnStatus = cs.calc(jobId , calc, error);
			this.insertErrorInfo(error);
		} catch (Exception e) {
			rtnStatus = 1;
			e.printStackTrace();
		}
		logger.debug(".............完成运行社保计算");
		return rtnStatus;
	}
	
	/**
	 * 添加消息推送
	 * @throws Exception 
	 */
	private void insertJMS(JmsService jms , String linkTo , String message , Long userId , Timestamp jobsTime) throws Exception{
		Jms bean = new Jms();
		bean.setJmsType(1L); //消息推送
		bean.setLinkTo(linkTo);
		bean.setJmsContent(message);
		bean.setUserId(userId);
		bean.setJobsTime(jobsTime);
		jms.save(bean);
	}
	
	/**
	 * 添加异常信息
	 * @param error
	 * @throws Exception
	 */
	private void insertErrorInfo(List<JobsMessage> error) throws Exception{
		if (DspUtil.isEmptyList(error)) return ;
		JobsMessageService jms = (JobsMessageService)DspUtil.getBean("jobsMessageService");
		for (JobsMessage jm : error) {
			jms.save(jm);
		}
	}
}
